package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ImageView;

import com.android.frame.ui.BaseFragment;
import com.android.frame.util.AppJsonUtil;
import com.android.frame.view.other.CustomViewPager;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.TopicDetailsFgtAdapter;
import com.kupurui.asteacher.bean.UnCorrectInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 题目详情
 * Created by Administrator on 2017/7/25.
 */

public class TopicDetailsAty extends BaseAty {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.viewpager)
    CustomViewPager viewpager;


    List<BaseFragment> fragments;
    TopicDetailsFgtAdapter fgtAdapter;
    List<UnCorrectInfo> list;

    private String id = "";
    private String class_id = "";
    private int fgtIndex = 0;

    @Override
    public int getLayoutId() {
        return R.layout.topic_details_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras()!=null){
            id = getIntent().getStringExtra("id");
            class_id = getIntent().getStringExtra("class_id");
            fgtIndex = getIntent().getExtras().getInt("fgtindex");
        }

        list = new ArrayList<>();
        fragments = new ArrayList<>();


        FragmentManager fm=getSupportFragmentManager();
        fgtAdapter = new TopicDetailsFgtAdapter(fm,fragments);
        viewpager.setCurrentItem(0);
        viewpager.setCanScroll(true);
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().question_detail(id,class_id,this,0);
    }

    @OnClick({R.id.iv_back})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                list.clear();
                list.addAll(AppJsonUtil.getArrayList(result,UnCorrectInfo.class));
                for (int i = 0; i <list.size() ; i++) {
                    Bundle bundle=new Bundle();
                    bundle.putInt("fgt_position",i);
                    if (i == list.size()-1){
                        bundle.putInt("size",1);
                    }else{
                        bundle.putInt("size",0);
                    }
                    bundle.putString("list_size",list.size()+"");
                    bundle.putSerializable("bean",list.get(i));
                    TopicDetailsFgt fgt=new TopicDetailsFgt();
                    fgt.setArguments(bundle);
                    fragments.add(fgt);
                }
                viewpager.setAdapter(fgtAdapter);
                viewpager.setCurrentItem(fgtIndex);
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    //手动设置ViewPager要显示的视图
    public void changeView(int desTab)
    {
        viewpager.setCurrentItem(desTab, true);
    }

}
